package fhp.hlhj.giantfold.activity.login
import android.annotation.SuppressLint
import android.os.Handler
import android.os.Message
import android.widget.Toast
import com.google.gson.Gson
import com.lzy.okgo.model.HttpParams
import fhp.hlhj.giantfold.BaseActivity
import fhp.hlhj.giantfold.R
import fhp.hlhj.giantfold.interfaces.IPhoneRegis
import fhp.hlhj.giantfold.javaBean.GetSmsBean
import fhp.hlhj.giantfold.javaBean.PhoneRegisBean
import fhp.hlhj.giantfold.presenter.PhoneRegisPresenter
import fhp.hlhj.giantfold.utils.LogUtil
import fhp.hlhj.giantfold.utils.MyUtils
import kotlinx.android.synthetic.main.activity_phone_regis_aty.*

class PhoneRegisAty : BaseActivity(), IPhoneRegis {
    private var sec = 60
    private val handler = @SuppressLint("HandlerLeak")
    object :Handler(){
        override fun handleMessage(msg: Message?) {
            super.handleMessage(msg)
            btGetYzm.isClickable=false
            sec--
            btGetYzm.text=sec.toString()+"秒"
            if (sec<1){
                btGetYzm.text="获取验证码"
                btGetYzm.isClickable=true
            }else{
                this.sendEmptyMessageDelayed(1,1000)
            }
        }
    }
    private lateinit var phoneRegisPresenter:PhoneRegisPresenter

    override fun getContentId(): Int = R.layout.activity_phone_regis_aty

    override fun initView() {
        phoneRegisPresenter= PhoneRegisPresenter(this)
    }

    override fun initListener() {
        btExit.setOnClickListener {
            finish()
        }
        btGetYzm.setOnClickListener {
            val params =HttpParams()
            params.put("phone",etPhone.text.toString())
            params.put("sms_key","47ec2dd791e31e2ef2076caf64ed9b3d")
            params.put("sms_type",0)
            phoneRegisPresenter.getSmsCode(params)
        }
        btCommit.setOnClickListener {
            val params = HttpParams()
            params.put("user_name",etPhone.text.toString())
            params.put("password",etPwd.text.toString())
            params.put("sms_code",etYzm.text.toString())
            params.put("client","android")
            phoneRegisPresenter.phoneRegis(params)
        }
    }
    /**
     * 加载
     */
    override fun showLoading() {
        loadingDialog.show()
    }
    /**
     * 获取验证码
     */
    override fun getSmsCode(getSmsBean: GetSmsBean) {
        loadingDialog.dismiss()
        LogUtil.log("短信"+Gson().toJson(getSmsBean))
        handler.sendEmptyMessage(1)
    }
    /**
     * 注册
     */
    override fun regis(phoneRegisBean: PhoneRegisBean) {
        loadingDialog.dismiss()
        LogUtil.log("完成"+phoneRegisBean.msg)
        if (phoneRegisBean.code==200){
            Toast.makeText(this,"注册成功",Toast.LENGTH_SHORT).show()
        }
    }
}
